package com.shunhao.network.newnet

import com.shunhao.network.BuildConfig
import com.shunhao.network.NetConstant
import com.shunhao.network.interceptor.InterceptorUtil
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.converter.scalars.ScalarsConverterFactory
import java.util.concurrent.TimeUnit

/**
 *类作用描述: 单例类 RetrofitHelper
 *@author      KangGuiYang 不指定apis
 *创建时间      2020/6/12 14:18
 *邮箱        kangguiyang2@gmail.com
 *公司        南京舜贝佳信息科技 Shunbega.com
 */
object RetrofitUtils {
    private const val DEFAULT_CONNECT_TIME: Long = 20
    private const val DEFAULT_READ_TIME_OUT: Long = 20
    private const val DEFAULT_WRITE_TIME_OUT: Long = 20

    private var mRetrofitClient: Retrofit
    private var mSingleJsonRetrofitClient: Retrofit

    init {
        mRetrofitClient = getRetrofitClient()
        mSingleJsonRetrofitClient = getRetrofitClient1()
    }


    fun <T> create(service: Class<T>): T = mRetrofitClient.create(service)

    fun <T> create1(service1: Class<T>): T = mSingleJsonRetrofitClient.create(service1)


    /**
     * 创建retrofit实例 返回格式化好的json对象
     */
    private fun getRetrofitClient(): Retrofit =
        Retrofit.Builder().apply {
            client(getOkHttpClient())
            baseUrl(NetConstant.BASE_URL)
            addConverterFactory(GsonConverterFactory.create())
            addCallAdapterFactory(RxJava2CallAdapterFactory.create())
        }.build()

    /**
     * 创建retrofit 返回 没有实例化的json对象 纯json字符串
     */
    private fun getRetrofitClient1(): Retrofit =
        Retrofit.Builder().apply {
            client(getOkHttpClient())
            baseUrl(NetConstant.BASE_URL)
            addConverterFactory(ScalarsConverterFactory.create())
            addCallAdapterFactory(RxJava2CallAdapterFactory.create())
        }.build()


    /**
     * 获取okHttpClient实例
     */
    private fun getOkHttpClient(): OkHttpClient {
       /* val sslParams = SSLInterceptor.getSslSocketFactory(
            arrayOf(
                Utils.getApp()
                    .assets.open(NetConstant.SSL_NAME)
            ), null, null
        )*/
        return OkHttpClient.Builder()
            .apply {
                connectTimeout(DEFAULT_CONNECT_TIME, TimeUnit.SECONDS)
                writeTimeout(DEFAULT_WRITE_TIME_OUT, TimeUnit.SECONDS)
                readTimeout(DEFAULT_READ_TIME_OUT, TimeUnit.SECONDS)
                // 设置出现错误进行重新连接
                retryOnConnectionFailure(true)

                if (BuildConfig.DEBUG) {
                    // log拦截器
                    addInterceptor(InterceptorUtil.logInterceptor())
                    // host拦截器
                    //addInterceptor(InterceptorUtil.HostInterceptor())

                }
                // 统一头部token拦截器
                addInterceptor(InterceptorUtil.headerInterceptor())
               // sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager)
            }.build()
    }
}